package com.zlzxm.studentscreen.ui.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.ProxyInfo;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.google.gson.Gson;
import com.tencent.mmkv.MMKV;
import com.zlzxm.studentscreen.R;
import com.zlzxm.studentscreen.globe.Constant;
import com.zlzxm.studentscreen.net.http.RxAndroidHelp;
import com.zlzxm.studentscreen.net.http.repository.MyRepository;
import com.zlzxm.studentscreen.net.http.response.BaseResponse;
import com.zlzxm.studentscreen.net.http.response.RpInfo;
import com.zlzxm.studentscreen.net.websocket.message.PlayContent;
import com.zlzxm.studentscreen.service.CoreService;
import com.zlzxm.studentscreen.ui.activity.manager.ActivityStarter;
import com.zlzxm.studentscreen.ui.activity.manager.BaseActivity;
import com.zlzxm.studentscreen.util.QRCodeUtil;
import com.zlzxm.studentscreen.util.ToastyUtil;
import com.zlzxm.studentscreen.util.ZViewHelp;
import com.zlzxm.studentscreen.util.deviceinfo.DeviceInfoUtil;

import io.reactivex.functions.Consumer;

/**
 * CREATE BY zlzxm ON 2022/3/9
 */
public class AcHome extends BaseActivity {
    MyRepository myRepository = new MyRepository();

    NetChange  netChange = new NetChange();
    TextView tvWifi;

    private ImageView ivAndroid,ivIos,ivShare;

    private LocalBroadcastManager localBroadcastManager;

    private LinearLayoutCompat llCode;
    private TextView tvCode;

    private BroadcastReceiver myMessageBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            if(intent.getAction().equals(CoreService.SEND_SCREEN_CODE)){
                //展示验证码
                llCode.setVisibility(View.VISIBLE);
                tvCode.setText(intent.getStringExtra(CoreService.SEND_SCREEN_CODE));
            }else   if(intent.getAction().equals(CoreService.PLAYER_SCREEN)){
                //展示验证码
                PlayContent playContent = (PlayContent) intent.getSerializableExtra(CoreService.PLAYER_SCREEN);

                ActivityStarter.startAcPlayer(AcHome.this,playContent.getUrl());

            }

        }
    };

    @Override
    public int loadRootView() {
        localBroadcastManager =  LocalBroadcastManager.getInstance(this);
        ZViewHelp.setStatusBarTransparent(getWindow(),false);
        return R.layout.ac_home;
    }

    @Override
    public void initView() {

        tvWifi = ZViewHelp.findById(this,R.id.tvWifi);

        ivAndroid= ZViewHelp.findById(this,R.id.ivAndroidQrCode);
        ivIos= ZViewHelp.findById(this,R.id.ivIosQrCode);
        ivShare= ZViewHelp.findById(this,R.id.ivQRCode);
        llCode= ZViewHelp.findById(this,R.id.llCode);
        tvCode= ZViewHelp.findById(this,R.id.tvCode);
    }

    @Override
    public void initClickListener() {

    }

    @Override
    public void initData() {
        llCode.setVisibility(View.GONE);
        getInfo();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (netChange == null) {
            netChange = new NetChange();
        }
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(netChange, filter);

        IntentFilter messageFilter = new IntentFilter();
        messageFilter.addAction(CoreService.SEND_SCREEN_CODE);
        messageFilter.addAction(CoreService.PLAYER_SCREEN);
        localBroadcastManager.registerReceiver(myMessageBroadcastReceiver,messageFilter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (netChange != null) {
            unregisterReceiver(netChange);
        }

        if(myMessageBroadcastReceiver!=null){
            localBroadcastManager.unregisterReceiver(myMessageBroadcastReceiver);
        }
    }


    private void getInfo(){

        myRepository.info(MMKV.defaultMMKV().decodeString(Constant.MMKVkey.DEVICE_ID))
                .compose(RxAndroidHelp.turn())
                .subscribe(new Consumer<BaseResponse<RpInfo>>() {
                    @Override
                    public void accept(BaseResponse<RpInfo> baseResponse) throws Exception {

                        if(baseResponse.getCode() == BaseResponse.SUCCEED){

                            String androidUrl = baseResponse.getData().getAndroidDownloadUrl();
                            String iosUrl = baseResponse.getData().getAndroidDownloadUrl();
                            Bitmap bitmapAndroid = QRCodeUtil.createQRCodeBitmap(androidUrl,200,200,null,null,null,
                                    Color.BLACK,Color.WHITE);
                            Bitmap bitmapIos = QRCodeUtil.createQRCodeBitmap(iosUrl,200,200,null,null,null,
                                    Color.BLACK,Color.WHITE);

                            ivAndroid.setImageBitmap(bitmapAndroid);
                            ivIos.setImageBitmap(bitmapIos);


                            Gson gson = new Gson();
                            String shareUrl = gson.toJson( baseResponse.getData().getQrCodeConnect());

                            Bitmap bitmapShare = QRCodeUtil.createQRCodeBitmap(shareUrl,200,200,null,null,null,
                                    Color.BLACK,Color.WHITE);

                            ivShare.setImageBitmap(bitmapShare);


                        }else {
                            errorToast(baseResponse.getMsg());
                        }


                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        errorToast("请求出异常了");
                    }
                });




    }


    public class NetChange extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if(DeviceInfoUtil.isWifiConnected(context)){

                DeviceInfoUtil.WifiInfoEntity entity = DeviceInfoUtil.getWifiInfo();

                if(entity != null ){

                    tvWifi.setText(DeviceInfoUtil.getSSID(context));

                }

            }else {

                tvWifi.setText("未知");

            }
        }

    }



}
